package javajavado.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javajavado.clases.Reunion;
import javajavado.excepcion.DAOExcepcion;
import javajavado.util.ConexionBD;

public class ReunionDAO extends BaseDAO{
	
	//REGISTRAR REUNIONES
		public Reunion insertar(Reunion vo) throws DAOExcepcion {
			String query = "insert into Reunion(Observacion,Calificacion,asesor_idasesor,Invitado_idInvitado) values (?,?,?,?)";
			Connection con = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setString(1, vo.getObservacion());
				stmt.setString(2, vo.getCalificacion());
				stmt.setInt(3, vo.getAsesorIdAsesor());
				stmt.setInt(4, vo.getInvitadoIdInvitado());
				
				int i = stmt.executeUpdate();
				if (i != 1) {
					throw new SQLException("No se pudo insertar");
				}
				// Obtener el ultimo id
				int id = 0;
				query = "select last_insert_id()";
				stmt = con.prepareStatement(query);
				rs = stmt.executeQuery();
				if (rs.next()) {
					id = rs.getInt(1);
				}
				vo.setIdReunion(id);

			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return vo;
		}
		
		// LISTAR REUNIONES
		public Collection<Reunion> listar() throws DAOExcepcion {
			Collection<Reunion> c = new ArrayList<Reunion>();
			Connection con = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				con = ConexionBD.obtenerConexion();
				String query = "select IdReunion,Observacion,Calificacion,asesor_idasesor,Invitado_idInvitado from Reunion order by IdReunion";
				stmt = con.prepareStatement(query);
				rs = stmt.executeQuery();
				while (rs.next()) {
					Reunion vo = new Reunion();
					vo.setIdReunion(rs.getInt("idReunion"));
					vo.setObservacion(rs.getString("Observacion"));
					vo.setCalificacion(rs.getString("calificacion"));
					vo.setAsesorIdAsesor(rs.getInt("asesorIdAsesor"));
					vo.setInvitadoIdInvitado(rs.getInt("invitadoIdInvitado"));
					c.add(vo);
				}

			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return c;
		}
		
		//OBTENER REUNIONES
		public Reunion obtener(int idReunion) throws DAOExcepcion {
			Reunion vo = new Reunion();
			Connection con = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				String query = "select IdReunion,Observacion,Calificacion,asesor_idasesor,InvitadoidInvitado from Reunion where idReunion=?";
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setInt(1, idReunion);
				rs = stmt.executeQuery();
				if (rs.next()) {
					vo.setIdReunion(rs.getInt(1));
					vo.setObservacion(rs.getString(2));
					vo.setCalificacion(rs.getString(3));
					vo.setAsesorIdAsesor(rs.getInt(4));
					vo.setInvitadoIdInvitado(rs.getInt(5));
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return vo;
		}

}
